package library.and.xps.com.step.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.SearchAdapter;
import library.and.xps.com.step.m.model.CheckJoinTeamPassRes;
import library.and.xps.com.step.m.model.QrScanRes;
import library.and.xps.com.step.m.model.Search;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.AppCommond;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseActivity;

public class JoinTeamActivity extends BaseActivity {
    TextView tv_tv_teamname, tv_orgname, tv_action_bar;
    ImageView iv_team_sign, iv_action_back;
    Button bt_next;
    EditText editText;
    int id;
    private static final String VERIFYPASS = "VERIFYPASS";
    public static final String ALLOWJOINTEAM = "ALLOWJOINTEAM";

    @Override
    protected int getLayout() {
        return R.layout.activity_jointeam;
    }

    @Override
    protected void setData() {
        Intent intent = getIntent();
        tv_action_bar.setText("加入团队");
        bt_next.setEnabled(false);

        Object o = intent.getSerializableExtra(SearchAdapter.Search_ListBean);

        if(o instanceof  Search.ListBean ){
            Search.ListBean listBean = (Search.ListBean) o;
            if (listBean == null) return;
            bt_next.setEnabled(true);
            bt_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    verifyPass();
                }
            });

            CommonUtils.setUpimg(this, iv_team_sign, listBean.getMark(),true);
            tv_tv_teamname.setText(listBean.getTname());
            tv_orgname.setText(listBean.getEnterprise());

            id = listBean.getTeamId();
        }else if(o instanceof QrScanRes.ObjBean){
            QrScanRes.ObjBean   listBean = (QrScanRes.ObjBean) o;
            if (listBean == null) return;
            bt_next.setEnabled(true);
            bt_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    verifyPass();
                }
            });

            CommonUtils.setUpimg(this, iv_team_sign, listBean.getMark(),true);
            tv_tv_teamname.setText(listBean.getTname());
            tv_orgname.setText(listBean.getEnterprise());

            id = listBean.getTeamid();
        }
    }

    void verifyPass() {
        String url = URLConstants.TEAM.JoinTeamVerify + "/?teamid=" + id + "&" + "password=" + editText.getText().toString();
        JUtils.Log(url);
        OkHttpClientUtils.getDataAsync(this, url, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                JUtils.Log(VERIFYPASS, "onFailure");
                dissmisDialog();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                dissmisDialog();
                JUtils.Log("JoinTeamActivity",response.getBody());
                CheckJoinTeamPassRes joinTeamRes = new Gson().fromJson(response.getBody(), CheckJoinTeamPassRes.class);

                if (HttpResponsHelper.SuccessResponce(joinTeamRes)) {
                    Intent i = new Intent(JoinTeamActivity.this, JoinTeamCompleteActivity.class);
                    i.putExtra(ALLOWJOINTEAM, joinTeamRes);
                    startActivity(i);
                }
            }

            @Override
            public void onStart() {
                showDialog();
            }
        }, VERIFYPASS);
    }

    @Override
    protected void init() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);

        tv_action_bar = r2v(R.id.actionbar_tv_title);
        tv_tv_teamname = r2v(R.id.tv_tname);
        tv_orgname = r2v(R.id.tv_orgname);
        bt_next = r2v(R.id.bt_next);
        iv_team_sign = r2v(R.id.iv_team_detail_avatar);
        editText = r2v(R.id.et_pass);
        iv_action_back = r2v(R.id.actionbar_iv_back);
        iv_action_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(AppCommond a) {
        if (a != null) {
            if (null != a.getAcNameList() && a.getOperation() == AppCommond.CLOSE_ACTIVITY && a.getAcNameList().size() > 0)
                for (String activityName : a.getAcNameList()) {
                    if (activityName.equals(this.getClass().getSimpleName())) {
                        finish();
                        break;
                    }
                }
        }
    }

}
